﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace Mirkuz.UI
{
    public class TComboSelection : TButton
    {
        private TComboItems _items;
        TComboItem _selecteditem;
        int SelectedIndex = 0;     

        public TComboSelection() :base()
        {
            _items = new TComboItems();
        }

        public TComboItems ComboItems
        {
            get
            {
                return _items;
            }
        }

        protected override void OnClick(EventArgs e)
        {
            if (ComboItems.Count == 0)
                return;

            SelectedIndex++;
            if (SelectedIndex >= ComboItems.Count)
            {
                SelectedIndex = 0;               
            }
            _selecteditem = ComboItems[SelectedIndex];
            Text = _selecteditem.ToString();
            this.PressedImage = _selecteditem.ItemImage;
            this.FocusedImage = _selecteditem.ItemImage;
            this.NormalImage = _selecteditem.ItemImage;

            base.OnClick(e);
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)
        {
            if (_selecteditem == null && ComboItems.Count != 0)            
                _selecteditem = ComboItems[0];

            if (_selecteditem!=null){
                Text = _selecteditem.Text;
                this.PressedImage = _selecteditem.ItemImage;
                this.FocusedImage = _selecteditem.ItemImage;
                this.NormalImage = _selecteditem.ItemImage;
            }
            base.OnPaint(pe);
        }

        public TComboItem SelectedItem
        {
            get
            {
                return _selecteditem;
            }
            set
            {
                _selecteditem = value;
                this.Invalidate();
            }
        }
    }


    public class TComboItem
    {
        string _value;
        public string Value { get { return _value; } }
        public String Text { get; set; }
        public Image ItemImage { get; set; }
        public TComboItem(string text){Text=text;}
        public TComboItem(string text, Image img) { Text = text; ItemImage = img; }
        public TComboItem(string text, Image img, string value) { Text = text; ItemImage = img; _value = value; }
        public override string ToString()
        {
            return Text;
        }
    }

    public class TComboItems : List<TComboItem>
    {
        public TComboItem this[string Value]
        {
            get{
                foreach (TComboItem i in this)
                    if (i.Value == Value)
                        return i;

                throw new Exception("Element not found in list");
            }
        }
    }
}
